package com.brillilab.starter.controller.aio.kit;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.lab.LabMemberPowerEnum;
import com.brillilab.domain.po.kit.ReagentType;
import com.brillilab.domain.po.kit.SampleTemplate;
import com.brillilab.domain.po.lab.LabMemberPower;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.kit.IReagentTypeService;
import com.brillilab.service.core.kit.ISampleTemplateService;
import com.brillilab.service.core.lab.ILabMemberService;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;

/**
 * 样品属性管理
 * 
 * @author lj
 *
 */
@RestController
@RequestMapping("/api/sample/attr")
public class SampleAttrController {
	@Autowired
	private UsersLogic usersLogic;
	@Autowired
	private ISampleTemplateService sampleTemplateService;
	@Autowired
	private IReagentTypeService reagentTypeService;
	@Autowired
	private ILabMemberService labMemberService;

	/**
	 * 获取样品属性模版
	 * 
	 * @param reagentTypeId 样品类型ID
	 * @return
	 */
	@RequestMapping(value = "/template", method = RequestMethod.GET)
	public ResponseVo getSampleType(@RequestHeader(name = StarterConstant.TOKEN) String token,
			@RequestParam Long reagentTypeId) {
		UserInfoVo urs = usersLogic.getUserByToken(token);
		return ResponseVo.success(
				sampleTemplateService.getSampleTemplateGroup(reagentTypeId, urs.getLabId(), urs.getLabMemberId()));
	}

	/**
	 * 添加样品属性
	 * 
	 * @return
	 */
	@AspectLog
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ResponseVo addTemplate(@RequestHeader(name = StarterConstant.TOKEN) String token,
			@RequestBody SampleTemplate templateValue) {
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		templateValue.setLabId(userVo.getLabId());
		templateValue.setLabMemberId(userVo.getLabMemberId());
		ReagentType reagentType = reagentTypeService.selectById(templateValue.getReagentTypeId());
		if(reagentType.getLabMemberId()!=null) {
			if (!userVo.getLabMemberId().equals(reagentType.getLabMemberId())) {
				Assert.isTrue(!CollectionUtils.isEmpty(userVo.getPowers()), "无权限新增该样品类型属性");
				List<String> powerList = userVo.getPowers().stream().map(LabMemberPower::getPower).collect(Collectors.toList());
				boolean contains = ArrayUtils.contains(powerList.toArray(), LabMemberPowerEnum.Power.REAGENT.getValue());
				Assert.isTrue(contains, "无权限新增该样品类型属性");
			}
		}
		templateValue.setIsSystem(0);
		templateValue.setIsSystemHide(1);
		SampleTemplate template = sampleTemplateService.insert(templateValue);	
		templateValue.setIsHide(0);
		sampleTemplateService.isHideTemplate(templateValue, userVo.getLabMemberId());
		return ResponseVo.success(template);
	}

	/**
	 * 修改样品属性
	 * 
	 * @param sampleTemplateId 样品属性ID
	 * @param name             名称
	 * @return
	 */
	@AspectLog
	@RequestMapping(value = "/modify", method = RequestMethod.GET)
	public ResponseVo modifyTemplate(@RequestHeader(name = StarterConstant.TOKEN) String token,
			@RequestParam Long sampleTemplateId, @RequestParam String name) {
		SampleTemplate template = sampleTemplateService.selectById(sampleTemplateId);
		Assert.isTrue(template != null, "样品属性不存在");
		Assert.isTrue(template.getIsSystem() != 1, "系统默认样品属性不能修改");
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		Assert.isTrue(userVo.getLabId().equals(template.getLabId()), "无权限修改该样品属性");
		
		boolean isOwner = labMemberService.isOwner(userVo.getLabId(), userVo.getLabMemberId());
		if (!isOwner) {
			ReagentType reagentType = reagentTypeService.selectById(template.getReagentTypeId());
			if (!userVo.getLabMemberId().equals(reagentType.getLabMemberId())
					&& !userVo.getLabMemberId().equals(template.getLabMemberId())) {
				Assert.isTrue(!CollectionUtils.isEmpty(userVo.getPowers()), "无权限修改该样品属性");
				List<String> powerList = userVo.getPowers().stream().map(LabMemberPower::getPower).collect(Collectors.toList());
				boolean contains = ArrayUtils.contains(powerList.toArray(), LabMemberPowerEnum.Power.REAGENT.getValue());
				Assert.isTrue(contains, "无权限修改该样品属性");
			}
		}
		template.setAttrName(name);
		boolean b = sampleTemplateService.updateById(template);
		return b ? ResponseVo.success() : ResponseVo.failure();
	}

	/**
	 * 显示/隐藏样品属性
	 * 
	 * @param sampleTemplateId 模版ID
	 * @param isHide           是否隐藏：0-否，1-是
	 * @return
	 */
	@RequestMapping(value = "/ishide", method = RequestMethod.GET)
	public ResponseVo hideTemplate(@RequestHeader(name = StarterConstant.TOKEN) String token,
			@RequestParam Long sampleTemplateId, @RequestParam Integer isHide) {
		Assert.isTrue(isHide == 0 || isHide == 1, "参数错误");
		SampleTemplate template = sampleTemplateService.selectById(sampleTemplateId);
		Assert.isTrue(template != null, "样品属性不存在");
		UserInfoVo urs = usersLogic.getUserByToken(token);
		template.setLabId(urs.getLabId());
		template.setIsHide(isHide);
		boolean b = sampleTemplateService.isHideTemplate(template, urs.getLabMemberId());
		return b ? ResponseVo.success() : ResponseVo.failure();
	}

	/**
	 * 删除样品属性
	 * 
	 * @param sampleTemplateId 样品属性ID
	 * @return
	 */
	@AspectLog
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public ResponseVo deleteTemplate(@RequestHeader(name = StarterConstant.TOKEN) String token,
			@RequestParam Long sampleTemplateId) {
		SampleTemplate template = sampleTemplateService.selectById(sampleTemplateId);
		Assert.isTrue(template != null, "样品属性不存在");
		Assert.isTrue(template.getIsSystem() != 1, "系统默认样品属性不能删除");
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		Assert.isTrue(userVo.getLabId().equals(template.getLabId()), "无权限删除该样品属性");

		boolean isOwner = labMemberService.isOwner(userVo.getLabId(), userVo.getLabMemberId());
		if (!isOwner) {
			ReagentType reagentType = reagentTypeService.selectById(template.getReagentTypeId());
			if (!userVo.getLabMemberId().equals(reagentType.getLabMemberId()) 
					&& !userVo.getLabMemberId().equals(template.getLabMemberId()) ) {
				Assert.isTrue(!CollectionUtils.isEmpty(userVo.getPowers()), "无权限删除该样品属性");
				List<String> powerList = userVo.getPowers().stream().map(LabMemberPower::getPower).collect(Collectors.toList());
				boolean contains = ArrayUtils.contains(powerList.toArray(), LabMemberPowerEnum.Power.REAGENT.getValue());
				Assert.isTrue(contains, "无权限删除该样品属性");
			}
		}
		boolean b = sampleTemplateService.deleteById(sampleTemplateId);
		return b ? ResponseVo.success() : ResponseVo.failure();
	}

}
